package com.woupla.account;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.New;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.PersistenceException;

import org.jboss.seam.transaction.Transactional;
import org.jboss.solder.logging.Logger;

import com.woupla.city.CityEJB;
import com.woupla.city.CityMBean;
import com.woupla.company.CompanyController;
import com.woupla.company.CompanyEJB;
import com.woupla.model.Company;
import com.woupla.model.User;

/**
 * @author Dimitri Lebel
 * 
 */
@Named
@ViewScoped
public class InscriptionMBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4445950074774092185L;
	@Inject
	private UserEJB userEJB;
	@Inject
	private Logger log;
	@New
	private User user;
	private String form;
	private String include;
	@Inject
	private CompanyController companyMBean;
	@Inject
	private CompanyEJB companyEJB;
	@Inject
	private CityMBean cityMBean;
	@Inject
	private CityEJB cityEJB;
	private boolean cvgOk;
	private String passWordConf;

	public InscriptionMBean() {
	}

	public void setUserRole() {
		if (form.contains("customerInscription")) {
			user.setRole(UserType.CUSTOMER.getValue());
		} else if (form.contains("managerInscription")) {
			user.setRole(UserType.MANAGER.getValue());
		} else if (form.contains("employeeInscription")) {
			user.setRole(UserType.EMPLOYEE.getValue());
		}
	}

	public boolean isManager() {
		if (form != null && form.contains("managerInscription")) {
			return true;
		}
		else
			return false;
	}

	@Transactional
	public String createUser() {
		if (!cvgOk) {
			FacesContext.getCurrentInstance().addMessage(
					"cvgCheck",
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Condition générale",
							"Vous devez lire et approuver les conditions générale."));
			return null;
		}
		try {
			if (!user.getPassword().equals(passWordConf)) {
				FacesContext.getCurrentInstance().addMessage(
						"confirm_password",
						new FacesMessage(FacesMessage.SEVERITY_ERROR,
								"#{bundles.i18n.inscription_mdp_erreur_titre}",
								"#{bundles.i18n.inscription_mdp_confirme}"));
				return null;
			}
			setUserRole();
			if (form.contains("managerInscription")) {
				Company company = companyMBean.getCompany();
				if (cityMBean.getCitySelected() != null) {

					company.getAdress().setCity(cityMBean.getCitySelected());
					companyEJB.saveCompany(company);
					user.setCompany(company);
					company.getEmployees().add(user);
					userEJB.saveUser(user);

				} else {
					FacesContext.getCurrentInstance().addMessage(
							null,
							new FacesMessage(FacesMessage.SEVERITY_ERROR,
									"Save Error", "Not a valid User"));
					return null;
				}
			} else {
				userEJB.saveUser(user);
			}
		} catch (PersistenceException e) {
			FacesContext.getCurrentInstance().addMessage(
					"Erreur",
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Erreur d'enregistrement",
							e.getMessage()));
			return null;
		}
		return "pretty:login";
	}

	@PostConstruct
	public void init() {
		user = new User();
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the form
	 */
	public String getForm() {
		return form;
	}

	/**
	 * @param form
	 *            the form to set
	 */
	public void setForm(String form) {
		this.form = form;
	}

	/**
	 * @return the include
	 */
	public String getInclude() {
		return include;
	}

	/**
	 * @param include
	 *            the include to set
	 */
	public void setInclude(String include) {
		this.include = include;
	}

	/**
	 * @return the passWordConf
	 */
	public String getPassWordConf() {
		return passWordConf;
	}

	/**
	 * @param passWordConf
	 *            the passWordConf to set
	 */
	public void setPassWordConf(String passWordConf) {
		this.passWordConf = passWordConf;
	}

	/**
	 * @return the cvgOk
	 */
	public boolean isCvgOk() {
		return cvgOk;
	}

	/**
	 * @param cvgOk
	 *            the cvgOk to set
	 */
	public void setCvgOk(boolean cvgOk) {
		this.cvgOk = cvgOk;
	}

}
